package com.gklyj.controller.qiuzhi;


import com.gklyj.pojo.Application;
import com.gklyj.pojo.Position;
import com.gklyj.pojo.User;
import com.gklyj.service.applicationService;
import com.gklyj.service.positionService;
import com.gklyj.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Transactional
public class Qz_wdsqController {

    @Autowired
    applicationService applicationService;

    @Autowired
    userService userService;

    @Autowired
    positionService positionService;
    /*
    正在审核的申请信息列表
     */
    @GetMapping("/ListApplication")
    @ResponseBody
    public List<Application> ListPositions(HttpServletRequest request){
        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        return applicationService.listShenhe(user.getU_id(),user.getName());
    }

    /*
    审核完成的申请信息列表
     */
    @GetMapping("/ListApplication1")
    @ResponseBody
    public List<Application> ListPositions1(HttpServletRequest request){
        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        return applicationService.listShenhe1(user.getU_id(),user.getName());
    }

    /*
    正在申请页面，根据条件查询申请信息
     */
    @PostMapping("/wdsq_search")
    @ResponseBody
    public List<Application> WdsqSearch(@RequestBody Application application,HttpServletRequest request){
        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        application.setU_id(user.getU_id());
        return applicationService.searchApplication(application);
    }

    /*
    审核完成页面，根据条件查询申请信息
     */
    @PostMapping("/wdsq_search1")
    @ResponseBody
    public List<Application> WdsqSearch1(@RequestBody Application application,HttpServletRequest request){
        Object username = request.getSession().getAttribute("username");
        User user = userService.findUserByUsername(String.valueOf(username));
        application.setU_id(user.getU_id());
        return applicationService.searchApplication1(application);
    }

    /*
    取消申请，并删除申请记录
     */
    @PostMapping("/cancelApplication")
    @ResponseBody
    public boolean cancelApply(@RequestBody Application application) throws RuntimeException{

        boolean b = positionService.ReducePositionQuantity("广东科技学院", application.getP_title());
        boolean c = applicationService.removeById(application.getA_id());
        if (b&&c){
            return true;
        }else {
            return false;
        }
    }

    /*
    点击修改申请后，弹出修改信息框
    */
    @GetMapping("/toWdsqEdit")
    public String toWdsqEdit(@RequestParam("id") String id, Model model){
        Application application = applicationService.getById(Integer.valueOf(id));
        model.addAttribute("detail",application);
        return "qiuzhi/qz-wdsq-Edit";
    }

    /*
    点击修改申请后，弹出修改信息框
    */
    @PostMapping("/applyEdit")
    @ResponseBody
    public boolean ApplyEdit(@RequestBody Application application) throws RuntimeException{
        return applicationService.updateById(application);
    }
}
